﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CGLib;
using CGLib.Enums;

namespace TienLen
{
    public partial class ResultsOffline : Form
    {
        // TODO: Redo results form.

        PictureBox[] medals = null;
        Label[] names = null;
        Label[] turns = null;
        Label[] combos = null;


        public ResultsOffline(Player[] players)
        {
            InitializeComponent();

            List<Results> places = new List<Results>();
            int playersonline = players.Length;

            for (int i = 0; i < players.Length; i++)
                places.Add(players[i].Results);

            medals = new PictureBox[4] { p1_medal, p2_medal, p3_medal, p4_medal };
            names = new Label[4] { p1_name, p2_name, p3_name, p4_name };
            turns = new Label[4] { p1_turn, p2_turn, p3_turn, p4_turn };
            combos = new Label[4] { p1_combo, p2_combo, p3_combo, p4_combo };


            for (int x = places.Count - 1; x >= 0; x--)
            {
                if (places[x].Place != 0)
                {
                    medals[places[x].Place - 1].Image = GetMedal(places[x].Place);
                    names[places[x].Place - 1].Text = players[x].Name;
                    turns[places[x].Place - 1].Text = GetTurnString(players[x], playersonline);

                    if (players[x].LeftGame)
                    {
                        playersonline--;
                        DisablePlace(x);
                    }

                    places.Remove(places[x]);
                }
            }

            if (places.Count != 0)
            {
                for (int x = 0; x < places.Count; x++)
                {
                    int placeGap = FindEmptyPlace();

                    names[placeGap].Text = players[x].Name;
                    turns[placeGap].Text = GetTurnString(players[x], playersonline);

                    if (players[x].LeftGame)
                    {
                        playersonline--;
                        DisablePlace(x);
                    }
                }
            }

            if (playersonline == 1 || SvrFunc.ServerType == SvrType.MSN)
            {
                buttonplay.Text = "Not implemented yet";
                buttonplay.Enabled = false;
            }
        }

        private void DisablePlace(int player)
        {
            medals[player - 1].Enabled = false;
            names[player - 1].Enabled = false;
            turns[player - 1].Enabled = false;
            combos[player - 1].Enabled = false;
        }

        private string GetTurnString(Player place, int numPlayers)
        {
            return place.LeftGame ? "Leaver!" : place.Results.Turns.ToString();
        }

        private int FindEmptyPlace()
        {
            for (int i = 0; i < 4; i++)
                if (string.IsNullOrEmpty(names[i].Text)) return i;

            return -1;
        }

        private Image GetMedal(int place)
        {
            switch (place)
            {
                case 1: return Image.FromFile(Application.StartupPath + @"\img\other\gold-mini.png");
                case 2: return Image.FromFile(Application.StartupPath + @"\img\other\silver-mini.png");
                case 3: return Image.FromFile(Application.StartupPath + @"\img\other\bronze-mini.png");
            }

            return null;
        }

        private void buttonok_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonplay_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
